package com.kakaxi.mybilibili.adapter;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;

import com.andexert.library.RippleView;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.kakaxi.mybilibili.R;
import com.kakaxi.mybilibili.bean.LiveItemBean;
import com.kakaxi.mybilibili.view.GridMarginDecoration;

import java.util.List;

/**
 * 分区直播间内容列表适配器
 * Created by kakaxi on 16/11/17.
 */

public class LiveCategoryRoomsContentRecyAdapter extends BaseQuickAdapter<LiveItemBean,
        BaseViewHolder> {

    private onRippleClickListener mListener;

    public LiveCategoryRoomsContentRecyAdapter(List<LiveItemBean> data) {
        super(R.layout.item_recy_live_content, data);
    }

    @Override
    protected void convert(final BaseViewHolder baseViewHolder, LiveItemBean liveItemBean) {
        baseViewHolder
                .setText(R.id.txt_live_content_title, liveItemBean.getTitle())
                .setText(R.id.txt_live_content_online, liveItemBean.getOnline() + "")
                .setText(R.id.txt_live_content_own_name, liveItemBean.getOwner().getName());
        Glide.with(mContext).load(liveItemBean.getCover().getSrc()).crossFade().into
                ((ImageView) baseViewHolder.getView(R.id.srimg_live_content_cover));

        RippleView rippleView = baseViewHolder.getView(R.id.rv_live_content_ripple);
        rippleView.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
            @Override
            public void onComplete(RippleView rippleView) {
                if (mListener != null) {
                    mListener.onRippleClick(rippleView, baseViewHolder.getLayoutPosition());
                }
            }
        });
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        if (recyclerView.getLayoutManager() == null) {
            GridLayoutManager layoutManager = new GridLayoutManager(recyclerView.getContext(), 2);
            recyclerView.setLayoutManager(layoutManager);
        }
        recyclerView.addItemDecoration(new GridMarginDecoration(recyclerView.getContext()));
    }

    public void setOnRippleClickListener(onRippleClickListener rippleClickListener) {
        mListener = rippleClickListener;
    }

    public onRippleClickListener getOnRippleClickListener() {
        return mListener;
    }

    /**
     * 波纹点击监听器
     */
    public interface onRippleClickListener {
        /**
         * 点击事件
         *
         * @param rippleView 视图
         * @param position   位置
         */
        void onRippleClick(RippleView rippleView, int position);
    }

}
